package org.example;

import org.testframe.*;

import java.io.File;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {

        new Tester().test();

    }

    public static class Tester extends FlowFunctions {

        public void test() {

            int runNum = 8;

            FlowBootStarter starter = FlowBootStarter.create(runNum, 100);

            //启动器开始
            starter.start();

            String chromedriverPath = new File("driver/chromedriver.exe").getAbsolutePath();

            //集合点对象
//            Gather gather = new Gather(30);
            for (int i = 0; i < runNum; i++) {
                starter.submit(

                        //开始流程
                        start()
                                .then(print("开始执行流程 {}", i))

                                //声明别称
                                .then(alias(elBySelector("body > app-root > layout-passport > div > div.wrap > passport-login > form > div > nz-form-item:nth-child(1) > nz-form-control > div > div > nz-input-group > input"),"用户名输入框"))

                                .then(alias(elBySelector("body > app-root > layout-passport > div > div.wrap > passport-login > form > div > nz-form-item:nth-child(2) > nz-form-control > div > div > nz-input-group > input"),"密码输入框"))

                                .then(alias(elBySelector("body > app-root > layout-passport > div > div.wrap > passport-login > form > div > nz-form-item.ant-row.ant-form-item.ng-star-inserted > nz-form-control > div > div > nz-input-group > span > span.ant-input-affix-wrapper.ant-input-affix-wrapper-lg.ng-star-inserted > input"),"验证码输入框"))

                                .then(alias(elBySelector("body > app-root > layout-passport > div > div.wrap > passport-login > form > nz-form-item:nth-child(4) > button > span"),"提交按钮"))

                                .then(alias(elBySelector("body > app-root > layout-basic > layout-default > layout-default-header > div > ul:nth-child(2) > li:nth-child(3) > header-user > div > div > i > svg"),"退出按钮"))

                                //初始化chrome浏览器
                                .then(initChrome(chromedriverPath))
                                //打开一个数据源
                                .then(openSource(csvFileSource("data/user.csv", 1), "用户名密码"))

                                .then(
                                        //循环流程
                                        loop(10,
                                                start()

                                                        // 进入登录页
                                                        .then(open("http://localhost/maxkey/#/passport/login"))

                                                        //等待60秒. 等待登录页标题的出现
                                                        .then(waiteElement(elByText("鲁商统一认证中心"), timeSeconds(60)))

                                                        //随机读取一行用户名密码. 取第一列为 username 第二列为 password
                                                        .then(readLine("用户名密码", ReadMode.random, "username=#1", "password=#2"))

                                                        // 格式化参数
                                                        .then(format(Formatters.trim, "username", "password"))


                                                        // 输入用户名和密码
                                                        .then(inputEl("用户名输入框", getValue("username")))
                                                        .then(inputEl("密码输入框", getValue("password")))

                                                        //验证码固定值
                                                        .then(inputEl("验证码输入框", "8888"))

//                                        .then(waiteGather(gather,timeSeconds(60)))

                                                        //然后点提交登录
                                                        .then(clickEl("提交按钮"))

                                                        //等待退出按钮的出现
                                                        .then(waiteElement("退出按钮", timeSeconds(10)))

                                                        //点击最后一个获取到的按钮
                                                        .then(clickEl(lastElement()))
                                        )
                                )

                                .then(waiteTime(timeSeconds(randomNumber(5))))
                                .then(print("完成流程 {}, 在执行数量 {}", i, js("ctx.countRunningFlow()")))
                                .errorAction(printError("执行异常!!!"))

                                //最后回收所有资源
                                .finallyAction(close())
                                .build()
                );

            }

        }


    }


}
